package com.ys.peony.ui.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.ys.peony.R;
import com.ys.peony.model.AreaBean;
import com.ys.peony.model.YWDJPrepareAreaItemModel;
import com.ys.peony.ui.adapter.SimpleTreeAdapter;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.StringUtils;
import com.zhy.tree.bean.Node;
import com.zhy.tree.bean.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区选择
 */
public class CarAreaCodePicker extends PopupWindow {
    private Context context;
    private View rootView;
    private CarAreaCodeCallBack callBack;

    private ListView optionLv;

    //listview 列表
    private List<YWDJPrepareAreaItemModel> options;
    private TreeListViewAdapter adapter;

    private List<AreaBean> mDatas = new ArrayList<AreaBean>();

    public CarAreaCodePicker(Context context, CarAreaCodeCallBack callBack, int width) {
        super();
        this.context = context;
        this.callBack = callBack;
        initView(width);
    }


    private void initView(int width) {
        rootView = LayoutInflater.from(context).inflate(R.layout.pop_typeval_layout_bak, null);
        setContentView(rootView);
        setWidth(width);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00ffffff);
        setBackgroundDrawable(dw);
        setOutsideTouchable(true);
        optionLv = (ListView) rootView.findViewById(R.id.option_lv);
//        adapter=new TypeValAdapter(context,options);
//        optionLv.setAdapter(adapter);
    }

    public interface CarAreaCodeCallBack {
        void carAreaCodeOnclick(String text, int val);
    }


    public void show(View mRootView, List<YWDJPrepareAreaItemModel> options) {
        this.options = options;
        setDates(options);
        try {
            adapter = new SimpleTreeAdapter<AreaBean>(optionLv, context, mDatas, 10);

            adapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
                @Override
                public void onClick(Node node, int position) {
                    if (node.isLeaf()) {
                        if (callBack != null) {
                            callBack.carAreaCodeOnclick(node.getName(), (int) (mDatas.get(position).getModel().getId()));
                        }
                        dismiss();
                    }
                }

            });

            optionLv.setAdapter(adapter);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //修改界面展示
        showAsDropDown(mRootView, 0, -PixelUtils.dip2px(10, context));
    }

    private void setDates(List<YWDJPrepareAreaItemModel> options) {
        mDatas.clear();
        setChildDates(options, 0);
    }


    private void setChildDates(List<YWDJPrepareAreaItemModel> options, int pid) {
        for (int i = 0; i < options.size(); i++) {
            if(StringUtils.isEmpty(options.get(i).getBrand())){
                mDatas.add(new AreaBean((int) options.get(i).getId(), pid, options.get(i).getName(), options.get(i)));
            }else{
                mDatas.add(new AreaBean((int) options.get(i).getId(), pid,options.get(i).getBrand()+"("+options.get(i).getName()+")", options.get(i)));
            }
            setChildDates(options.get(i).getSysAreaList(), (int) options.get(i).getId());
        }
    }
}
